package com.shangxian.giveithome;
import com.shangxian.giveithome.test.QrTest;

import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class asd {
    static final String URL = "https://app.shangxianruanjian.cn/page?id=";
    static final String path= "D:"+File.separator+"picFolder1";
    static final boolean DEBUG= false;
    public static void main(String[] args) {
        BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(100);
        ThreadPoolExecutor executor =new  ThreadPoolExecutor(8,8,1, TimeUnit.DAYS,queue);
        for (Integer j = 0; j <= 100; j++) {
            try {
                File f1 = new File(path);
                if (!f1.exists()) {
                    f1.mkdirs();
                }
                QRThread lt = new QRThread();
                lt.setFile(f1);
                lt.setUrl(URL+j);
                int i = j.toString().length();
                String filename = "";
                if (i==1){
                    filename = "00000"+j;
                }else if (i==2){
                    filename = "0000"+j;
                }else if (i==3){
                    filename = "000"+j;
                }else if (i==4){
                    filename ="00"+j;
                }else if(i==5){
                    filename = "0"+j;
                }
                lt.setFilename(filename+".png");
                executor.execute(lt);
                Thread.sleep(20);
                if (DEBUG) {
                    System.out.println("线程池中线程数目："+executor.getPoolSize()+
                            "，队列中等待执行的任务数目："+
                            executor.getQueue().size()+"，已执行玩别的任务数目："
                            +executor.getCompletedTaskCount());
                    System.out.println(Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory());
                }
            } catch (Exception e) {
                System.out.println(e.getMessage() + "," + j);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
            }
        }
        executor.shutdown();
        try {
            boolean loop = true;
            do {
                //等待所有任务完成
                //阻塞，直到线程池里所有任务结束
                loop = !executor.awaitTermination(2, TimeUnit.SECONDS);
            } while(loop);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
class QRThread extends Thread{
    private File file;
    private String userId;
    private String filename;
    private String url;
    @Override
    public void run() {
        try {
            QrTest.createCodeToFile(url,file,filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public File getFile() {
        return file;
    }
    public void setFile(File file) {
        this.file = file;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
}
